<?php

namespace App\Admin\Controllers;

use App\Models\TcmFoodDetail;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TcmFoodDetailController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new TcmFoodDetail(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            //$grid->column('shareImg');
            $grid->column('picture')->image('',50,50);
            //$grid->column('meridian');
            //$grid->column('taste');
            //$grid->column('application');
            //$grid->column('composition');
            $grid->column('classification');
            $grid->column('natures');
            $grid->column('effects');
            $grid->column('flavors');
            //$grid->column('concretEffects');
            //$grid->column('contraindication');
            //$grid->column('created_at');
            //$grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new TcmFoodDetail(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('shareImg')->image('',50,50);
            $show->field('picture')->image('',50,50);
            $show->field('meridian')->json();
            $show->field('taste');
            $show->field('application')->json();
            $show->field('composition')->json();
            $show->field('classification');
            $show->field('natures')->json();
            $show->field('effects')->json();
            $show->field('flavors')->json();
            $show->field('concretEffects')->json();
            $show->field('contraindication')->json();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new TcmFoodDetail(), function (Form $form) {
			$form->tab('基本信息', function (Form $form) {
				$form->display('id');
				$form->text('name');
				$form->text('classification');
				$form->image('picture')->uniqueName()->autoUpload()->saveFullUrl();
				$form->image('shareImg')->uniqueName()->autoUpload()->saveFullUrl();
			});
			$form->tab('性味功效标签', function (Form $form) {		
				$form->list('natures')->saving(function ($v) {
					return json_encode($v,JSON_UNESCAPED_UNICODE);
				});
				$form->list('effects')->saving(function ($v) {
					return json_encode($v,JSON_UNESCAPED_UNICODE);
				});
				$form->list('flavors')->saving(function ($v) {
					return json_encode($v,JSON_UNESCAPED_UNICODE);
				});
				$form->list('concretEffects')->saving(function ($v) {
					return json_encode($v,JSON_UNESCAPED_UNICODE);
				});
			});
			$form->tab('性味功效', function (Form $form) {
				$form->array('taste','', function ($table) {
					$table->text('title','标题');
					$table->editor('content','内容');
				})->saving(function ($v) {
					return json_encode($v,JSON_UNESCAPED_UNICODE);
				});
			});
            $form->tab('归经', function (Form $form) {
				$form->embeds('meridian','', function ($table) {
					$table->text('title','标题');
					$table->text('content','内容');
					$table->image('images','图片')->uniqueName()->autoUpload()->saveFullUrl();
				})->saving(function ($v) {
					return json_encode($v,JSON_UNESCAPED_UNICODE);
				});
			});
			
			$form->tab('应用', function (Form $form) {
				$form->array('application','', function ($table) {
					$table->text('disease','疾病');
					$table->textarea('basis','成分');
				})->saving(function ($v) {
					return json_encode($v,JSON_UNESCAPED_UNICODE);
				});
			});
			
			 $form->tab('营养成分', function (Form $form) {
				$form->table('composition','', function ($table) {
					$table->text('name','名称');
					$table->text('content','重量');
				})->saving(function ($v) {
					return json_encode($v,JSON_UNESCAPED_UNICODE);
				});
			});
		
			$form->tab('人群宜忌', function (Form $form) {
				$form->embeds('contraindication','', function ($table) {
					$table->text('suitableConstitutions','宜用体质');
					$table->text('suitableDiseases','宜用病症');
					$table->text('unsuitableConstitutions','慎用体质');
					$table->text('unsuitableDiseases','慎用病症');

				})->saving(function ($v) {
					return json_encode($v,JSON_UNESCAPED_UNICODE);
				});
			});
           
        });
    }
}
